package com.wink.admin.controller;


import com.wink.admin.common.response.ApiResponse;
import com.wink.admin.entity.SysUser;
import com.wink.admin.utils.ResponseUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;


/**
 * 测试控制器，已重写springMVC参数解析器，@RequestBody 也可以支持表单提交的参数封装
 * PS:实际使用中并不会这么写，和前端会统一请求方式和参数位置的，添加的原因就是手写json很烦
 *
 * @author xw
 * @date 2020/10/27 13:48
 */
@Slf4j
@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {


    @PostMapping("/abc")
    public ApiResponse abc(SysUser sysUser) {
        log.info("user:{}", sysUser);
        return ResponseUtils.ok();
    }

    @PostMapping("/efg")
    public ApiResponse efg(@RequestBody SysUser sysUser) {
        log.info("user:{}", sysUser);
        return ResponseUtils.ok();
    }

    @PostMapping("/cc")
    public ApiResponse cc(MultipartFile file) {
        log.info("file:{}", file.getOriginalFilename());
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try (InputStream inputStream = file.getInputStream()) {
            reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            log.warn("getBodyString出现问题！");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error("读取requestInputStream异常：", e);
                }
            }
        }
        log.info("请求的body内容为:{}", sb.toString());
        return ResponseUtils.ok();
    }
}
